//
//  MHMomentViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMomentViewModel.h"
#import "MHProfileInfoViewModel.h"
#import "MHWebViewModel.h"
#import "MHTestViewModel.h"
#import "MHRecentStory.h"
#import "MHComposeViewModel.h"
#import "MHRepoViewModel.h"
#import "MHMomentDetailViewModel.h"
#import "MHAwemeListViewModel.h"
#import "MHDetailLocationViewModel.h"
@interface MHMomentViewModel ()
/// 个人信息头部视图模型
@property (nonatomic, readwrite, strong) MHMomentProfileViewModel *profileViewModel;

@property(nonatomic, assign) int type; //是否精华，全部=0，精华=1  recentType 1表示动态，2表示提问

@property(nonatomic, assign) int circleId;

@property(nonatomic, assign) BOOL isColleciton;




/// 刷新某一个section的 事件回调
@property (nonatomic, readwrite, strong) RACSubject *reloadSectionSubject;

/// 电话号码回调
@property (nonatomic, readwrite, strong) RACSubject *phoneSubject;

/// 评论回调
@property (nonatomic, readwrite, strong) RACSubject *commentSubject;
/// 发送评论内容
@property (nonatomic, readwrite, strong) RACCommand *commentCommand;
/// 删除当前用户的评论
@property (nonatomic, readwrite, strong) RACCommand *delCommentCommand;
/// 删除当前用户的发的说说
@property (nonatomic, readwrite, strong) RACCommand *delMomentCommand;




//// 跳转用户信息的命令
@property (nonatomic, readwrite, strong) RACCommand *profileInfoCommand;

/// 富文本文字上的事件处理
@property (nonatomic, readwrite, strong) RACCommand *attributedTapCommand;
/// 分享view上的点击事件处理
@property (nonatomic, readwrite, strong) RACCommand *shareTapCommand;
@end


@implementation MHMomentViewModel



- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.type = [params[MHViewModelUtilKey] intValue];
        self.circleId = [params[MHViewModelIDKey] intValue];
        self.isDetail = [params[MHViewModelIsDetail] boolValue];
        self.isColleciton = [params[MHViewModelIsCollection] boolValue];
        self.isFootPrint = [params[MHViewModelIsFootPrint] boolValue];
        self.conversation = params[MHViewModelConversation];
    }
    return self;
}



- (void)initialize{
    [super initialize];
    
    @weakify(self);
    
    
    
    /// 设置tableView的样式
    self.style = UITableViewStyleGrouped;
    /// 隐藏导航栏的细线
    self.prefersNavigationBarBottomLineHidden = YES;
    
    /// 允许下拉刷新+上拉加载
    self.shouldPullDownToRefresh = YES;
    self.shouldPullUpToLoadMore = YES;
    
    /// 显示的数据是每页八条说说
    self.perPage = 8;
    
    /// 允许多段显示
    self.shouldMultiSections = YES;
    self.shouldEndRefreshingWithNoMoreData = NO;
    
    //    self.shouldEndRefreshingWithNoMoreData
    
    /// 个人信息头部视图
    self.profileViewModel = [[MHMomentProfileViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:self.services.client.currentUser}];
    
    /// 事件操作
    //// 初始化一系列subject
    self.reloadSectionSubject = [RACSubject subject];
    self.reloadDataSubject = [RACSubject subject];
    self.commentSubject = [RACSubject subject];
    self.phoneSubject = [RACSubject subject];
    
    if(self.isFootPrint)
    {
        self.rightTitle = @"发私信";
        self.prefersNavigationBarHidden = false;
    }
    
//    if (!_isDetail) {
//        RAC(self, moments) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
//        RAC(self,dataSource) = [RACObserve(self, moments) map:^(NSArray * moments) {
//            @strongify(self)
//            return [self dataSourceWithMoment:moments];
//        }];
//        
//    }
    
    
    
    /// 评论
    self.commentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(MHMomentReplyItemViewModel * itemViewModel) {
        @strongify(self);
        //        MHMomentCommentItemViewModel *commentItemViewModel = [[MHMomentCommentItemViewModel alloc] initWithComment:comment];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[itemViewModel.section];
        /// 配置评论内容
        //属于提问
        if( momentItemViewModel.moment.recentQA.questionContent.length > 0 && momentItemViewModel.moment.recentQA.answerContent.length <= 0 && momentItemViewModel.moment.recentQA.answerUid == [MHHTTPService sharedInstance].currentUser.userId && !momentItemViewModel.isTheme )
        {
            return [[[[MHHTTPService sharedInstance] answerCircleQuestionsWithRecentId:momentItemViewModel.moment.autoId content:itemViewModel.text lon:0 lat:0] mh_parsedResults] doNext:^(MHMoment *moment) {
                @strongify(self);
                MHMomentItemViewModel *momentItemViewModelTemp = [[MHMomentItemViewModel alloc] initWithMoment:moment];
                momentItemViewModelTemp.attributedTapCommand = self.attributedTapCommand;
                momentItemViewModelTemp.reloadSectionSubject = self.reloadSectionSubject;
                momentItemViewModelTemp.commentSubject = self.commentSubject;
                momentItemViewModelTemp.isDetail = YES;
                self.message.recentId = moment.autoId;
                self.momentId = moment.autoId;
                self.moments = @[moment];
                //                self.comments = @[];
                [momentItemViewModelTemp _updateSubviewsFrameWithExpand:self.isDetail];
                //                [momentItemViewModelTemp updateUpArrow];
                self.dataSource = @[momentItemViewModelTemp];
//                IMAMsg *msg = [IMAMsg msgWithCustom:EIMAMSG_Custom param:[moment yy_modelToJSONString]];
//                [[NSNotificationCenter defaultCenter] postNotificationName:kIMAMSG_SendMsgNotification object:msg];
                
                
                //                [self.reloadSectionSubject sendNext:@(itemViewModel.section)];
            }];
        }else
        {
            MHComment *comment = [[MHComment alloc] init];
            comment.text = itemViewModel.text;
            /// 搞个随机字符串
            comment.idstr = [NSString stringWithFormat:@"%ld",[NSObject mh_randomNumberWithFrom:10000000 to:99999999]];
            comment.momentIdstr = itemViewModel.momentIdstr;
            comment.createdAt = [NSDate date];
            comment.fromUser = self.services.client.currentUser;
            if (itemViewModel.toUser) {
                comment.isReply = YES;
                comment.parentId = itemViewModel.parentId;
            }
            
            comment.toUser = itemViewModel.toUser;
            
            
            
            /// 刷新数据源
            
            //            self.dataSource = self.dataSource;
            return [[[[MHHTTPService sharedInstance] publishCommentWithRecentId:momentItemViewModel.moment.autoId content:comment.text parentId:comment.isReply ? comment.parentId : 0] mh_parsedResults] doNext:^(MHComment *comentResponse) {
                
                if (![comentResponse isKindOfClass:[MHComment class]]) {
                    return ;
                }
                /// 根据 comment 获取到 commentItemViewModel
                MHMomentCommentItemViewModel *commentItemViewModel = [[MHMomentCommentItemViewModel alloc] initWithComment:comentResponse];
                commentItemViewModel.attributedTapCommand = self.attributedTapCommand;
                
                /// 根据section 获取到 momentItemViewModel
                
                
                /// 插入数据
                [momentItemViewModel.dataSource addObject:commentItemViewModel];
                [momentItemViewModel.moment.commentsList addObject:comentResponse];
                //                momentItemViewModel.moment.commentsCount += 1;
                
                /// 更新headerView的upArrow
                [momentItemViewModel updateUpArrow];
                [self.reloadSectionSubject sendNext:@(itemViewModel.section)];
                
            }];
            //            MHMomentItemViewModel *momentItemViewModel = self.dataSource[itemViewModel.section];
            
        }
    }];
    
    
    self.repoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        int  index  =  [input intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        MHRepoViewModel *repo  =  [[MHRepoViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:momentItemViewModel.moment}];
        [self.services pushViewModel:repo animated:YES];
        return [RACSignal empty];
        //        @strongify(self);
        //        int  index  =  [section intValue];
        //        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        //        return [self.services.client reportRecentStoryWithRecentId:momentItemViewModel.moment.autoId reportTxt:@""];
    }];
    
    /// 删除评论
    self.delCommentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        @strongify(self);
        /// 根据indexPath.section 获取到 momentItemViewModel
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[indexPath.section];
        MHMomentCommentItemViewModel *commentItemViewModel = momentItemViewModel.dataSource[indexPath.row];
        /// 删除数据
        [momentItemViewModel.dataSource removeObjectAtIndex:indexPath.row];
        /// 这个数据源 这里使用 removeObject来删除，一次来避免索引引起的Bug
        [momentItemViewModel.moment.commentsList removeObject:commentItemViewModel.comment];
        
        
        //        momentItemViewModel.moment.commentsCount -= 1;
        
        /// 更新headerView的upArrow
        [momentItemViewModel updateUpArrow];
        /// 刷新数据源
        [self.reloadSectionSubject sendNext:@(indexPath.section)];
        return [self.services.client deleteCommentWith:commentItemViewModel.comment.autoId];
    }];
    
    //删除主题
    self.delMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        NSMutableArray *source =   [self.dataSource mutableCopy];
        [source removeObject:momentItemViewModel];
        self.dataSource = [NSArray arrayWithArray:source]; //[source copy];
        return [self.services.client deleteRecente:momentItemViewModel.moment.autoId];
    }];
    self.topMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        
        
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        
        return [[self.services.client topRecente:momentItemViewModel.moment.autoId] doNext:^(id x) {
            for (MHMomentItemViewModel *itme in self.dataSource) {
                if (itme.moment.isTop) {
                    itme.moment.isTop = false;
                    NSInteger index =  [self.dataSource indexOfObject:itme];
                    [self.reloadSectionSubject sendNext:@(index)];
                }
            }
            momentItemViewModel.moment.isTop = YES;
            //删除元素，插入到最前面
            NSMutableArray *source =   [self.dataSource mutableCopy];
            [source removeObject:momentItemViewModel];
            [source insertObject:momentItemViewModel atIndex:0];
            self.dataSource = [NSArray arrayWithArray:source];
            [self.reloadSectionSubject sendNext:section];
            
        }];
    }];
    self.cancelTopMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        momentItemViewModel.moment.isTop = false;
        //是什么位置就回到什么位置； //排序规则
        
        //        NSMutableArray *source =   [self.dataSource mutableCopy];
        //        [source removeObject:momentItemViewModel];
        //        [source insertObject:momentItemViewModel atIndex:0];
        //        self.dataSource = [NSArray arrayWithArray:source];
        return [[self.services.client cancelTopRecente:momentItemViewModel.moment.autoId] doNext:^(id x) {
            [self.reloadSectionSubject sendNext:section];
        }];
    }];
    
    self.setEssenceMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        momentItemViewModel.moment.isEssence = YES;
        
        return [self.services.client essenceRecente:momentItemViewModel.moment.autoId];
    }];
    
    
    self.cancelEssenceMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        momentItemViewModel.moment.isEssence = false;
        return [self.services.client cancelEssenceRecente:momentItemViewModel.moment.autoId];
    }];
    
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *input) {
        @strongify(self);
        
        if (!self.isDetail) {
            int section = [input intValue];
            MHMomentItemViewModel *momentItemViewModel =  self.dataSource[section];
            if (momentItemViewModel.moment.videoInfo) {
                
                if (self.isTheme || self.isColleciton || self.isRecent) {
                    
                    if(self.isColleciton)
                    {
                        
                        MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[],MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false, @"MHViewModelCollectionList":@(self.isColleciton)}];
                        [self.services pushViewModel:viewModel animated:YES];
                        
//                        return   [[[self.services.client getRecentVideoPagedListByUserCollectWithMarkId:momentItemViewModel.moment.autoId pageNum:1 pageSize:self.perPage] mh_parsedResults] doNext:^(NSArray *modeArr) {
//                            MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:modeArr,MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false, @"MHViewModelCollectionList":@(self.isColleciton)}];
//                            [self.services pushViewModel:viewModel animated:YES];
//                        }];
                        
                    }
                    else{
                        if (self.isRecent) {
                            momentItemViewModel.moment.circleId = 0;
                        }
                        MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[],MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false,@"MHViewModelHiddenEnter":@(self.isTheme), @"MHViewModelCollectionList":@(self.isColleciton),@"MHViewModelTimeLineList":@(self.isRecent)}];
                        [self.services pushViewModel:viewModel animated:YES];
                        return [RACSignal empty];
                        
//                        return  [[[self.services.client getRecentPagedListByNewVideoTimeDescWithRecentId:momentItemViewModel.moment.autoId circleId: self.isRecent ? 0 : [AXAppDelegate sharedDelegate].currentCircleInfo.autoId pageNum:1 pageSize:self.perPage] mh_parsedResults] doNext:^(NSArray *modelArr) {
//                            if (modelArr.count) {
//                                MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:modelArr,MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false, @"MHViewModelCollectionList":@(self.isColleciton),@"MHViewModelTimeLineList":@(self.isRecent)}];
//                                [self.services pushViewModel:viewModel animated:YES];
//                            }
//                        }];
                    }
                    
                    
                }else
                {
                    return   [[[self.services.client getRecentInfoByNewVideoWithRecentId:momentItemViewModel.moment.autoId] mh_parsedResults] doNext:^(MHAwemeModel *model) {
                        MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[model],@"MHViewModelShowEnterBtn":@false,@"MHViewModelShowSingleVideo":@true}];
                        [self.services pushViewModel:viewModel animated:YES];
                    }];
                }
                
            }else
            {
                int32_t recentId;
                if (self.isAskList && momentItemViewModel.moment.recentQA.answerContent.length > 0) {
                    recentId =  momentItemViewModel.moment.quoteRecentId;
                }else
                {
                    recentId =  self.isColleciton ? momentItemViewModel.moment.recentId                                                                                                                                                   : momentItemViewModel.moment.autoId;
                }
                
                MHMomentDetailViewModel *detail = [[MHMomentDetailViewModel alloc] initWithServices:self.services params:@{MHViewModelIDKey:@(recentId),MHViewModelIsDetail:@(true)}];
                [self.services pushViewModel:detail animated:YES];
            }
        }
        return [RACSignal empty];
    }];
    
    
    
    
    self.modifyMomentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        
        //修改动态
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        //动态
        MHComposeViewModel *compose = [[MHComposeViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:momentItemViewModel.moment}];
        
        [self.services presentViewModel:compose animated:YES completion:nil];
        return [RACSignal empty];
    }];
    
    self.collectionCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *dic) {
        BOOL isSelected =  [dic[@"isCollection"] boolValue];
        int  index  =  [dic[@"section"] intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        if (isSelected) {
            //取消收藏
            if(momentItemViewModel.isCollection)
            {
                return [[[MHHTTPService sharedInstance] cancelCollectMarkWithRecentId:momentItemViewModel.moment.recentId] doNext:^(id x) {
                    NSMutableArray *source =   [self.dataSource mutableCopy];
                    [source removeObject:momentItemViewModel];
                    self.dataSource = [NSMutableArray arrayWithArray:source];
                }];
            }else
            {
                return [[MHHTTPService sharedInstance] cancelCollectMarkWithRecentId:momentItemViewModel.moment.autoId] ;
            }
            
        }else{
            
            return [[MHHTTPService sharedInstance] addRecentCollectMarkWithRecentId:momentItemViewModel.moment.autoId];
        }
        
    }];
    
    
    self.profileInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(IMAUser  *user) {
        @strongify(self);
        if (!self.isFootPrint) {
//            IMAUser *user = [[IMAUser alloc] initWith:[NSString stringWithFormat:@"%d",model.]];
//            user.icon = model.nickName;
//            user.nickName = model.nickName;
//            user.userId =
                IMAConversation *conversation =  [[IMAPlatform sharedInstance].conversationMgr chatWith:user];
                MHMomentViewModel *viewModel = [[MHMomentViewModel alloc] initWithServices:self.services params:@{MHViewModelIsFootPrint:@(true),MHViewModelTitleKey:isEquaalCurrentUserId(user.userId)?@"我的足迹":@"Ta的足迹",MHViewModelConversation:conversation}];
                viewModel.userId = [user.userId intValue];
                viewModel.imUser = user;
                [self.services pushViewModel:viewModel animated:YES];

        }
        return [RACSignal empty];
    }];
    
    
    self.expandOperationCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *section) {
        @strongify(self);
        int  index  =  [section intValue];
        MHMomentItemViewModel *momentItemViewModel = self.dataSource[index];
        //        MHMomentItemViewModel *momentItemViewModel =  self.dataSource[section];
        if (momentItemViewModel.moment.videoInfo) {
            
            if (self.isTheme || self.isColleciton) {
                if(self.isColleciton)
                {
                    MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[],MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false, @"MHViewModelCollectionList":@(self.isColleciton)}];
                    [self.services pushViewModel:viewModel animated:YES];
                    return [RACSignal empty];
//                    return   [[[self.services.client getRecentVideoPagedListByUserCollectWithMarkId:momentItemViewModel.moment.autoId pageNum:1 pageSize:self.perPage] mh_parsedResults] doNext:^(NSArray *modeArr) {
//                        MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:modeArr,MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false, @"MHViewModelCollectionList":@(self.isColleciton)}];
//                        [self.services pushViewModel:viewModel animated:YES];
//                    }];
                    
                }else
                {
                    
                    MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[],MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false}];
                    [self.services pushViewModel:viewModel animated:YES];
                    return [RACSignal empty];
//                    return  [[[self.services.client getRecentPagedListByNewVideoTimeDescWithRecentId:momentItemViewModel.moment.autoId circleId:[AXAppDelegate sharedDelegate].currentCircleInfo.autoId pageNum:1 pageSize:self.perPage] mh_parsedResults] doNext:^(NSArray *modelArr) {
//                        MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:modelArr,MHViewModelCommonModelUtilKey:momentItemViewModel.moment,@"MHViewModelShowEnterBtn":@false}];
//                        [self.services pushViewModel:viewModel animated:YES];
//                    }];
                }
            }
            else
            {
                return   [[[self.services.client getRecentInfoByNewVideoWithRecentId:momentItemViewModel.moment.autoId] mh_parsedResults] doNext:^(MHAwemeModel *model) {
                    MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[model],@"MHViewModelShowEnterBtn":@false,@"MHViewModelShowSingleVideo":@true}];
                    [self.services pushViewModel:viewModel animated:YES];
                }];
            }
        }else
        {
            int32_t recentId;
            if (self.isAskList && momentItemViewModel.moment.recentQA.answerContent.length > 0) {
                recentId =  momentItemViewModel.moment.quoteRecentId;
            }else
            {
                recentId =  self.isColleciton ? momentItemViewModel.moment.recentId                                                                                                                                                   : momentItemViewModel.moment.autoId;
            }
            
            MHMomentDetailViewModel *detail = [[MHMomentDetailViewModel alloc] initWithServices:self.services params:@{MHViewModelIDKey:@(recentId),MHViewModelIsDetail:@(true)}];
            [self.services pushViewModel:detail animated:YES];
        }
        //    }
        return [RACSignal empty];
    }];
    
    /// 内容文本上高亮点击事件
    self.attributedTapCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary * userInfo) {
        @strongify(self);
        
        if (userInfo[MHMomentUserInfoKey]) { /// 用户
            
            MHUser *model =  userInfo[MHMomentUserInfoKey];
            IMAUser *user = [[IMAUser alloc] initWith:[NSString stringWithFormat:@"%d",model.userId]];
            user.icon = model.allHeadPath;
            user.nickName = model.nickName;
            [self.profileInfoCommand execute:user];
            return [RACSignal empty];
        }
        
        if (userInfo[MHMomentLinkUrlKey]) { /// 链接
            NSURL *url = [NSURL URLWithString:userInfo[MHMomentLinkUrlKey]];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            MHWebViewModel *viewModel = [[MHWebViewModel alloc] initWithServices:self.services params:@{MHViewModelRequestKey:request}];
            [self.services pushViewModel:viewModel animated:YES];
            return [RACSignal empty];
        }
        
        if (userInfo[MHMomentLocationNameKey]) { /// 地理位置
            MHDetailLocationViewModel *locationViewModel =  [[MHDetailLocationViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:userInfo[MHMomentLocationNameKey]}];
            [self.services pushViewModel:locationViewModel  animated: YES];
            return [RACSignal empty];
        }
        
        if (userInfo[MHMomentPhoneNumberKey]) { /// 电话号码
            [self.phoneSubject sendNext:userInfo[MHMomentPhoneNumberKey]];
            return [RACSignal empty];
        }
        
        return [RACSignal empty];
    }];
    
    
    self.pushToChatView = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [[IMAPlatform sharedInstance] asyncGetStrangerInfo:[self.conversation receiver] succ:^(IMAUser *auser) {
            MHChatViewModel *viewModel = [[MHChatViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:auser}];
            [self.services pushViewModel:viewModel animated:YES];
        } fail:^(int code, NSString *msg) {
            
        }];
        return [RACSignal empty];
    }];
    
    self.shareTapCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(MHMomentShareInfo *shareInfo) {
        @strongify(self);
        //        NSURL *url = [NSURL URLWithString:shareInfo.url];
        //        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        //        MHWebViewModel *viewModel = [[MHWebViewModel alloc] initWithServices:self.services params:@{MHViewModelRequestKey:request}];
        //        [self.services pushViewModel:viewModel animated:YES];
        return [RACSignal empty];
    }];
}





- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    @weakify(self);
    NSArray * (^mapMoments)(NSArray *) = ^(NSArray *moments) {
//        self
     
        NSArray *viewModels = @[];
        /// 转化数据
        if (moments && moments.count) { ///做个判断
            viewModels = [self dataSourceWithMoment:moments];
        }
        
        if (page==1) {
            self.dataSource = viewModels ?:@[];
        }else{
            //// 类似于拼接
            self.dataSource = @[ (self.dataSource ?:@[]).rac_sequence , viewModels.rac_sequence].rac_sequence.flatten.array;
        }
        return viewModels;
        
    };
    int  isEssence = 0;
    int recentType = 0;
    
    // 2是提问
    if (self.type > 1 || self.isRecent == 1) {
        recentType = self.type;
    }else
    {
        isEssence = self.type;
    }

    if (self.isColleciton) {
        return  [[self.services.client fetchCollectMarkPagedListWithCircleId:self.circleId?:0 pageNum:page pageSize:self.perPage] map:mapMoments];
    } else if (self.isAnserList)
    {
        return [[self.services.client getUserAnswerPagedListWithCircleId:self.circleId userId:self.userId pageNum:page pageSize:self.perPage] map:mapMoments];
    }else if (self.isAskList)
    {
        return [[self.services.client getUserQuestionPagedListWithCircleId:self.circleId pageNum:page pageSize:self.perPage] map:mapMoments];
    }else if (self.isFootPrint)
    {
        return [[self.services.client fetchUserExpePagedListWithUserId:self.userId pageNum:page pageSize:self.perPage] map:mapMoments];
    }
    else
    {
        return  [[self.services.client fetchRecentStoryPagedListWithCircleId:self.circleId userId:self.userId isEssence:isEssence recentType:recentType  pageNum:page pageSize:self.perPage] map:mapMoments];
    }
    
}


#pragma mark - 辅助方法
- (NSArray *)dataSourceWithMoment:(NSArray *)moments {
    if (MHObjectIsNil(moments) || moments.count == 0) return nil;
    NSMutableArray *viewModels = [[moments.rac_sequence map:^(MHMoment *moment) {
        MHMomentItemViewModel *itemViewModel = [[MHMomentItemViewModel alloc] initWithMoment:moment];
        itemViewModel.isDetail = self.isDetail;
        itemViewModel.isRecent = self.isRecent;
        itemViewModel.isRealateToMe = self.isRealateToMe;
        itemViewModel.didSelectCommand = self.didSelectCommand;
        itemViewModel.isCollection = self.isColleciton;
        itemViewModel.isTheme = self.isTheme;
        itemViewModel.isFootPrint = self.isFootPrint;
        itemViewModel.collectionCommand = self.collectionCommand;
        itemViewModel.expandOperationCmd = self.expandOperationCmd;
        itemViewModel.reloadSectionSubject = self.reloadSectionSubject;
        itemViewModel.commentSubject = self.commentSubject;
        itemViewModel.profileInfoCommand = self.profileInfoCommand;
        itemViewModel.attributedTapCommand = self.attributedTapCommand;
        itemViewModel.shareTapCommand = self.shareTapCommand;
        [itemViewModel _updateSubviewsFrameWithExpand:self.isDetail];
        return itemViewModel;
    }].array mutableCopy];
 
    return viewModels ?: @[] ;
}

@end
